package net.frankish.moviebank2.front;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import net.frankish.moviebank.beans.Movie;
import net.frankish.moviebank.util.Translator;
import net.frankish.tools.GuiTools;

public class MoviePanel extends JPanel{

	private static final long serialVersionUID = 20080623L;
	private static final Logger logger = Logger.getLogger(MoviePanel.class.getName());
	Object currentData = null;
	//GUI Components

	private JPanel coverPanel = new JPanel();
	private Thread loadCoverThread = null;

	private JTextField fieldTitle = new JTextField();
	private JTextField fieldProducer = new JTextField();
	private JTextField fieldRelease = new JTextField();
	private JTextArea fieldDescription = new JTextArea(5,10);
	private JTextArea listActors = null;
	private GenrePanel genrePanel = null;
	private JPanel leftPanel = null;
	private JPanel rightPanel = null;
	private Movie currentMovie;
	
	public MoviePanel(){
		super();
		guiInit();
	}

	private void guiInit(){
		JLabel labelTitle = new JLabel(Translator.get("edit.movie.title"));
		JLabel labelProducer = new JLabel(Translator.get("edit.movie.producer"));
		JLabel labelRelease = new JLabel(Translator.get("edit.movie.release"));
		JLabel labelActors = new JLabel(Translator.get("edit.movie.actor"));
		JLabel labelGenres = new JLabel(Translator.get("edit.movie.genre"));
		fieldDescription.setWrapStyleWord(true);
		fieldDescription.setLineWrap(true);
		JScrollPane pane = new JScrollPane(fieldDescription);
		pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		pane.setBorder(BorderFactory.createTitledBorder(Translator.get("edit.movie.description")));
		listActors = new JTextArea();
		
		leftPanel = new JPanel(new GridBagLayout());
		rightPanel = new JPanel(new GridBagLayout());
		genrePanel = new GenrePanel();
		genrePanel.refresh();
		genrePanel.setEnabled(false);
		fieldTitle.setEditable(false);
		fieldProducer.setEditable(false);
		fieldRelease.setEditable(false);
		listActors.setEditable(false);
		fieldDescription.setEditable(false);
		
		leftPanel.add(labelTitle,new GridBagConstraints(0,0,2,1,1.0, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 1, 1));
		leftPanel.add(fieldTitle,new GridBagConstraints(0,1,2,1,1.0, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 5, 2), 1, 1));
		leftPanel.add(labelProducer,new GridBagConstraints(0,2,1,1,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 1, 1));
		leftPanel.add(fieldProducer,new GridBagConstraints(0,3,1,1,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 5, 2), 1, 1));
		leftPanel.add(labelRelease,new GridBagConstraints(1,2,1,1,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 1, 1));
		leftPanel.add(fieldRelease,new GridBagConstraints(1,3,1,1,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 5, 2), 1, 1));	
		leftPanel.add(labelActors,new GridBagConstraints(0,6,1,1,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 1, 1));
		leftPanel.add(listActors,new GridBagConstraints(0,7,1,2,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 5, 2), 1, 1));
		leftPanel.add(labelGenres,new GridBagConstraints(1,6,1,1,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 1, 1));
		leftPanel.add(genrePanel,new GridBagConstraints(1,7,2,1,0.5, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 5, 2), 1, 1));
		leftPanel.add(new JPanel(),new GridBagConstraints(0,10,2,1,1.0, 0.8, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		coverPanel.setBorder(BorderFactory.createLoweredBevelBorder());
		rightPanel.add(coverPanel,new GridBagConstraints(0,0,1,1,1.0,0.7, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 1, 1));
		rightPanel.add(pane,new GridBagConstraints(0,1,1,1,1.0,0.4, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 1, 1));
		
		setLayout(new GridBagLayout());
		this.add(leftPanel ,new GridBagConstraints(0,0,1,1,0.5,1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 1, 1));
		this.add(rightPanel,new GridBagConstraints(1,0,1,1,0.5,1.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 1, 1));
	}

	public void load(Movie bean){
		this.currentMovie = bean;
		logger.fine("Loading movie :"+bean);
		if(loadCoverThread != null){
			if(loadCoverThread.isAlive()){
				loadCoverThread.interrupt();
				loadCoverThread= null;
			}
		}
		fieldTitle.setText("");
		fieldDescription.setText("");
		fieldProducer.setText("");
		fieldRelease.setText("");
		listActors.setText("");
		coverPanel.removeAll();

		if(bean != null){
			listActors.setText(bean.getActors());
			fieldTitle.setText(bean.getTitle());
			fieldDescription.setText(bean.getDescription());
			fieldProducer.setText(bean.getProducer());
			fieldRelease.setText(bean.getRelease());
			genrePanel.selectGenre(bean.getGenre());

			coverPanel.removeAll();
			loadCoverThread = new Thread(new PictureRendererThread(bean.getCoverPicture(), bean.getImage(),  this.coverPanel));
			loadCoverThread.start();

		}
		GuiTools.setSizes(leftPanel, new Dimension(this.getMaximumSize().width/2 -15,this.getMaximumSize().height));
		GuiTools.setSizes(rightPanel, new Dimension(this.getMaximumSize().width/2 -15,this.getMaximumSize().height));
		GuiTools.setSizes(coverPanel, new Dimension(this.getMaximumSize().width/2 -20,this.getMaximumSize().height/2 - 25));
		this.revalidate();
	}

	/**
	 * Setting a MoviePanel editable makes all of its fields editable and display the picture cover controls.
	 * @param b
	 */
	public void setEditable(boolean b) {
		logger.info("Setting MoviePanel editable["+b+"] for Movie["+this.currentMovie+"]");
	}
	
}
